# -*- coding: utf-8 -*-
"""The base Controller API

Provides the BaseController class for subclassing.
"""
from pylons.controllers import WSGIController
from pylons.templating import render_mako as render
from pylons import request, response, session, tmpl_context as c

from craneserver.model.meta import Session

class BaseController(WSGIController):

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        identity = environ.get('repoze.who.identity')
        if identity:
            c.identity = identity
            c.userid = identity['repoze.who.userid']
            c.isadmin = 'Super User' in list(c.identity['groups'])
        else:
            c.identity = None
            c.userid = None
            c.isadmin = False
        try:
            return WSGIController.__call__(self, environ, start_response)
        finally:
            Session.remove()
